#!/usr/bin/
#-*- coding: utf-8 -*-
#File: MooControllerViews
#Author: Iván Viguera Cáceres
#Last revision: 27/03/2011
#Description:
#   Define controller views class.

from MooControllerConn import MooControllerConn
from MooControllerCalendar import MooControllerCalendar
from MooControllerLogin import MooControllerLogin
from MooControllerTasks import MooControllerTask
from events import EventManager, Event



class MooDelegateView (EventManager):
    
    def __init__(self, views = None, name_list = None):
        '''
        @var views: equivale a un diccionario. Nombre_vista : controlador_vista
        @var name_list: lista de nombres de todas las vistas a controlar.
        '''
        EventManager.__init__(self)
        
        self.conn = MooControllerConn('http://192.168.1.35/moodle/wspp/wsdl_pp.php')
        self.views = views
        self.active_views = {}
        
    def init_view(self, *args):
        if args[0] not in self.active_views.keys():
            instance = self.views[args[0]](self, *args)
            #Agrega la instancia del controlador de vista al diccionario.
            self.active_views[args[0]] = instance
            instance.start()
        else:
            try:
                self.active_views[args[0]].refrescar_vista()
            except:
                print 'No existe el método...aún'
            
            
    def connect_event_view(self, name_view, function):
        event = Event(name_view)
        #agrega el evento a la lista de eventos
        self.add_event(event)
        #connecta la señal a la función que se desea ejecutar del manipulador
        self.connect(name_view, function)
    
    def close_views(self):
        for view in self.active_views.values():
            self.remove_view(view.name)
            view.close()
        print '54'
    def remove_view(self, name_view):
        try:
            self.active_views.pop(name_view)
        except:
            pass
        
if __name__ == "__main__":
    #main de pruebas
    
    views = {'login' : MooControllerLogin,
             'calendario' : MooControllerCalendar,
             'task' : MooControllerTask }
    
    controlView = MooDelegateView(views)
    controlView.connect_event_view('login',controlView.init_view)
    controlView.init_view('login')
        
    print 'Aplicación finalizada'
